{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chap7 用户输入和while语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "大多数程序都是需要从用户获取一些信息，在本章中，你将会学习到如何接收用户输入的信息。因此我会向你介绍input()函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你还会学习到如何不断重复代码内容的语句。因此我会向你介绍while()函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1 input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please input a number:11\n",
      "\n",
      " the number is 11\n"
     ]
    }
   ],
   "source": [
    "num_str = input(\"please input a number:\")\n",
    "print(\"\\n the number is \" + num_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "input()会让程序等待一会，等待用户输入。获取用户输入后，你需要存储到一个变量里，方便你的使用。**注意**input()的返回值默认是字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此，强制类型转换是必要的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please input a number:18\n",
      "you have a vote\n"
     ]
    }
   ],
   "source": [
    "num_str = num_str = input(\"please input a number:\")\n",
    "num = int(num_str)\n",
    "if num >= 18:\n",
    "    print('you have a vote')\n",
    "else:\n",
    "    print('you are too young')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.2 while()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for语句针对的是集合中的每一个元素都是代码块，while是若判断成立，则一直执行下去。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "while(a <= 5):\n",
    "    print(a)\n",
    "    a += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当a = 1时， a <= 5成立，所以执行print语句并a = a + 1 = 2；<br>\n",
    "当a = 2时， a <= 5成立，所以执行print语句并a = 3；<br>\n",
    "当a = 3时， a <= 5成立, 所以执行print语句并a = 4；<br>\n",
    "当a = 4时， a <= 5成立，所以执行print语句并a = 5；<br>\n",
    "当a = 5时,  a <= 5成立，所以执行print语句并a = 6；<br>\n",
    "**当a = 6时， a <= 5不成立，结束循环。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 通过设置循环结束标志其实也是种不错的选择。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当情况很多时，没有循环结束标志就会显得代码很乱，我们可以用结束标志代替判断情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "flag = True\n",
    "num = 1\n",
    "while flag:\n",
    "    print(num)\n",
    "    num += 1\n",
    "    if num > 9:\n",
    "        flag = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用break退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "flag = True\n",
    "num = 1\n",
    "while flag:\n",
    "    print(num)\n",
    "    num += 1\n",
    "    if num > 9:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "break的意思是不执行下面的语句直接退出循环。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "front\n",
      "quit\n"
     ]
    }
   ],
   "source": [
    "flag = True\n",
    "while flag:\n",
    "    print('front')\n",
    "    break\n",
    "    print('back')\n",
    "\n",
    "print('quit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正如你所看到的没有打印back"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* continue同样是不执行下面的语句但不会退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "current_number = 0\n",
    "while current_number < 10: \n",
    "    current_number += 1\n",
    "    if current_number % 2 == 0:\n",
    "        continue\n",
    "    print(current_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "它没有打印偶数，因为continue语句起作用了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.3 用while处理列表和字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Verifying user: Candace\n",
      "Verifying user: Brian\n",
      "Verifying user: Alice\n",
      "\n",
      "The following users have been confirmed:\n",
      "Candace\n",
      "Brian\n",
      "Alice\n"
     ]
    }
   ],
   "source": [
    "# 首先，创建一个待验证用户列表\n",
    "# 和一个用于存储已验证用户的空列表\n",
    "unconfirmed_users = ['alice', 'brian', 'candace']\n",
    "confirmed_users = []\n",
    "\n",
    "# 验证每个用户，直到没有未验证用户为止\n",
    "# 将每个经过验证的列表都移到已验证用户列表中\n",
    "while unconfirmed_users:\n",
    "    current_user = unconfirmed_users.pop()\n",
    "    print(\"Verifying user: \" + current_user.title())\n",
    "    confirmed_users.append(current_user)\n",
    "    \n",
    "# 显示所有已验证的用户\n",
    "print(\"\\nThe following users have been confirmed:\")\n",
    "for confirmed_user in confirmed_users:\n",
    "    print(confirmed_user.title())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个判断条件是nuconfirmed_users是否为空"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.4 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你学习了：\n",
    "1. 如何在程序中使用input() 来让用户提供信息；\n",
    "2. 如何处理文本和数字输入;\n",
    "3. 如何使用while 循环让程序按用户的要求不断地运行；\n",
    "4. 多种控制while 循环流程的方式：设置活动标志、使用break 语句以及使用continue 语句；\n",
    "5. 如何使用while 循环在列表之间移动元素;\n",
    "6. 如何结合使用while 循环和字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
